﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Web.UI.WebControls
    <DefaultProperty("Text"), ToolboxData("<{0}:DateValidator runat=server></{0}:DateValidator>")> _
    Public Class DateValidator
        Inherits BaseValidator

        Public Overridable Property Format() As String
            Get
                Return CStr(ViewState("Format"))
            End Get
            Set(ByVal value As String)
                ViewState("Format") = value
            End Set
        End Property

        Protected Overrides Function EvaluateIsValid() As Boolean
            Dim value As String = GetControlValidationValue(ControlToValidate)
            If value Is Nothing OrElse value = "" Then
                Return True
            End If
            Dim format_ As String = Format
            Try
                If format_ Is Nothing OrElse format_ = "" Then
                    Dim tempDate As Date = Date.Parse(value)
                Else
                    Dim tempDate As Date = Date.ParseExact(value, format_, Nothing)
                End If
                Return True
            Catch ex As FormatException
                Return False
            End Try
        End Function

        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            MyBase.OnPreRender(e)
            Page.ClientScript.RegisterClientScriptResource(Me.GetType(), "Itvirtuoso.date.js")
        End Sub
    End Class
End Namespace
